<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="流程名称">
              <a-input placeholder="名称" v-model="queryParam.name"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="流程Key">
              <a-input placeholder="Key" v-model="queryParam.key"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-button @click="() => {this.queryPage()}" type="primary">查询</a-button>
            <a-button @click="() => queryParam = {}" style="margin-left: 8px">重置</a-button>
            <a-button style="margin-left: 8px" @click="$refs.AddFile.open()"> <a-icon type="upload" />点击上传</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      bordered
      ref="table"
      :alert="alert"
      :data="loadData"
      :columns="columns"
      :rowKey="record => record.id"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
      <template v-slot:action="record">
        <span v-if="record.publish!=='publishTrue'">
          <a-popconfirm title="是否发布该流程" @confirm="publish(record.id)" okText="是" cancelText="否">
            <a-icon slot="icon" type="question-circle-o" style="color: red" />
            <a href="javascript:" style="color: red">发布</a>
            <a-divider type="vertical" />
          </a-popconfirm>
        </span>
        <span >
          <a href="javascript:" @click="start(record)">启动</a>
          <a-divider type="vertical" />
        </span>
      </template>
      <template v-slot:pic="record">
        <span >
          <a href="javascript:" @click="showPicOrXml(record.defId,'pic')">图片</a>
        </span>
    </template>
      <template v-slot:xml="record">
        <span >
          <a href="javascript:" @click="showPicOrXml(record.modelEditorXml,'xml')">xml</a>
        </span>
      </template>
    </s-table>
    <instance-start
      @ok="queryPage"
      ref="InstanceStart"
    />
    <add-file
      @ok="queryPage"
      ref="AddFile"
    />
    <show-pic-or-xml
      @ok="queryPage"
      ref="ShowPicOrXml"
    />
  </a-card>
</template>

<script>
import { fetchList, publish } from '@/api/process/model'
import InstanceStart from '../instance/InstanceStart'
import { TableMixin } from '@/mixins/TableMixin'
import AddFile from './AddFile'
import ShowPicOrXml from './ShowPicOrXml'

export default {
  name: 'ModelList',
  components: {
    InstanceStart,
    AddFile,
    ShowPicOrXml
  },
  mixins: [TableMixin],
  data () {
    return {
      columns: [
        {
          title: '序号',
          customRender: (text, record, index) => {
            return (
              <div>
                {index + 1}
              </div>
            )
          }
        }, {
          title: '名称',
          dataIndex: 'name'
        }, {
          title: '流程类型',
          dataIndex: 'typeKey'
        }, {
          title: '发布状态',
          dataIndex: 'publish',
          customRender: (text, record, index) => {
            return text === 'publishTrue' ? '已发布' : '未发布'
          }
        }, {
          title: '流程key',
          dataIndex: 'defKey'
        },
        {
          title: '流程图',
          scopedSlots: { customRender: 'pic' }
        },
        {
          title: 'xml',
          scopedSlots: { customRender: 'xml' }
        }, {
          title: '是否主流程',
          dataIndex: 'mainProcess',
          customRender: (text, record, index) => {
            return text ? '是' : '否'
          }
        }, {
          title: '流程版本号',
          dataIndex: 'processVersion'
        }, {
          title: '操作',
          width: '200px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      loadData: parameter => {
        return fetchList(
          Object.assign(parameter, this.queryParam)
        ).then(res => {
          return res.data
        })
      }
    }
  },
  methods: {
    /**
       * 发布
       */
    publish (modelId) {
      publish(modelId).then(res => {
        this.$message.info(res.msg)
        this.queryPage()
      })
    },
    /**
       * 启动
       */
    start (record) {
      this.$refs.InstanceStart.init(record.id, 'add')
    },
    showPicOrXml (value, type) {
      this.$refs.ShowPicOrXml.init(value, type)
    }
  }
}
</script>

<style scoped>

</style>
